<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SystemConfig extends BaseModel
{
    protected $table = 'system_config';
    protected $nav_table = 'system_nav_menu';
    protected $shortcutWordTable = 'shortcut_word';
    protected $blackWordTable = 'black_word';
    protected $blackWordLogTable = 'black_word_log';

    public $timestamps = false;
    protected $fillable = ['key','value','desc','type','category'];
    private $fields = ['*'];

    public function getList(Request $request){
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $key = $request->input('key', '');
        $category = $request->input('category', '');

        // todo 搜索
        $where = [];
        if ($key) {
            $where['key'] = $key;
        }
        if ($category) {
            $where['category'] = $category;
        }

        $result = self::query()->where($where)->orderBy('id','DESC')->paginate($limit, $this->fields, 'page', $page);
        $result = objToArray($result);
        if(!empty($result['data'])) {
            foreach($result['data'] as $key=>$val) {
                if($val['key'] === 'grasslandsheep_white') {
                    $res = curl_request(getKkaudioActivityUrl() . '/inner/grasslandSheep/getGrasslandSheepWhiteNow', [], 'urlencoded', 'GET');
                    $res = curlResultHandle($res);
                    if($res['code'] != 20000) {
                        return [];
                    }
                    $result['data'][$key]['value'] = $res['data']['winner'];
                }
            }
        }

        return $result;
    }


    //添加
    public function createData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/system/config/create', $data, 'json', 'POST');
    }

    //更新
    public function updateData($data, $user)
    {
        if(isset($data['key']) && $data['key']==='grasslandsheep_white') {
            $data = [
                'admin_id' => $user['id'],
                'winner' => $data['value'],
            ];
            $res = curl_request(getKkaudioActivityUrl() . '/inner/grasslandSheep/setGrasslandSheepWhite', $data, 'urlencoded', 'POST');
            return $res;
        }

        return curl_request(getKkaudioApiUrl() . '/inner/system/config/update', $data, 'json', 'POST');
    }

    //删除
    public function removeData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/system/config/remove', $data, 'urlencoded', 'GET');
    }

    public function navList(){
        return [
            ['id' => 1 , 'key'=> 'activeIcon' , 'desc' => '动态'],
            ['id' => 2 , 'key'=> 'recreateIcon' , 'desc' => '娱乐'],
            ['id' => 3 , 'key'=> 'messageIcon' , 'desc' => '消息'],
            ['id' => 4 , 'key'=> 'myIcon' , 'desc' => '我的'],
            ['id' => 5 , 'key'=> 'matchingIcon' , 'desc' => '匹配'],
            ['id' => 6 , 'key'=> 'refreshIcon' , 'desc' => '刷新']
        ];
    }

    public function getNavList(){
        $res = DB::connection($this->connection)->table($this->nav_table)
            ->get();
        return $this->objToArray($res);
    }

    public function getNavByKey($key){
        $res = DB::connection($this->connection)->table($this->nav_table)
            ->where('key',$key)
            ->first();
        return $this->objToArray($res);
    }

    //快捷语列表
    public function listShortcutWord(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $data_type = $request->input('data_type');
        $content = $request->input('content');

        $query = DB::connection($this->connection)->table($this->shortcutWordTable)->where('is_delete', 0);
        if(!empty($data_type)) {
            $query = $query->where('data_type', $data_type);
        }
        if(!empty($content)) {
            $query = $query->where('content', 'like', '%'.$content.'%');
        }
        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        if(!empty($result['data'])) {
            $adminIds = array_column($result['data'], 'admin_id');
            $userList = DB::table('users')->whereIn('id', $adminIds)->select('id','name')->get();
            $userList = array_column(objToArray($userList), null, 'id');

            foreach($result['data'] as $key=>$val) {
                $val['admin_name'] = empty($userList[$val['admin_id']]['name']) ? '' : $userList[$val['admin_id']]['name'];

                $result['data'][$key] = $val;
            }
        }

        return $result;
    }

    //添加快捷语
    public function addShortcutWord($data){
        $user = Users::getCurrentUser();
        $data['admin_id'] = $user->id ?? 0;
        $res = curl_request(getKkaudioApiUrl() . '/inner/system/addShortcutWord', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //编辑快捷语
    public function editShortcutWord($data){
        $user = Users::getCurrentUser();
        $data['admin_id'] = $user->id ?? 0;
        $res = curl_request(getKkaudioApiUrl() . '/inner/system/editShortcutWord', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //删除快捷语
    public function delShortcutWord($data){
        $user = Users::getCurrentUser();
        $data['admin_id'] = $user->id ?? 0;
        $res = curl_request(getKkaudioApiUrl() . '/inner/system/delShortcutWord', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //黑名单语言列表
    public function listBlackWord(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $word_desc = $request->input('word_desc');
        $promptly_type = $request->input('promptly_type');

        $query = DB::connection($this->connection)->table($this->blackWordTable)->where('is_delete', 0);
        if(!empty($word_desc)) {
            $query = $query->where('word_desc', 'like', '%'.$word_desc.'%');
        }
        if(!empty($promptly_type)) {
            $query = $query->where('promptly_type', $promptly_type);
        }
        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        if(!empty($result['data'])) {
            $adminIds = array_column($result['data'], 'admin_id');
            $userList = DB::table('users')->whereIn('id', $adminIds)->select('id','name')->get();
            $userList = array_column(objToArray($userList), null, 'id');

            foreach($result['data'] as $key=>$val) {
                $val['admin_name'] = empty($userList[$val['admin_id']]['name']) ? '' : $userList[$val['admin_id']]['name'];

                $result['data'][$key] = $val;
            }
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

    //添加黑名单语言
    public function addBlackWord($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/system/addBlackWord', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //编辑黑名单语言
    public function editBlackWord($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/system/editBlackWord', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //删除黑名单语言
    public function delBlackWord($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/system/delBlackWord', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

}
